package src.main.java.com.pms.util;

import java.util.Scanner;

public class InputUtil {

    private static final Scanner scanner = new Scanner(System.in);
    private static final int MAX_RETRY_TIMES = 3;  // 最大重试次数

    public static int getInt(String prompt, int min, int max) {
        int retryCount = 0;
        while (retryCount < MAX_RETRY_TIMES) {
            System.out.print(prompt);
            try {
                String input = scanner.nextLine().trim();
                if (input.isEmpty()) {
                    System.out.println("输入不能为空，请重试。");
                    retryCount++;
                    continue;
                }
                int value = Integer.parseInt(input);
                if (value >= min && value <= max) {
                    return value;
                }
                System.out.printf("输入必须在 %d 到 %d 之间，请重试。\n", min, max);
            } catch (NumberFormatException e) {
                System.out.println("无效的数字格式，请重试。");
            }
            retryCount++;
        }
        throw new RuntimeException("已达到最大重试次数");
    }
    /**
     * 获取用户输入的整数
     *
     * @param prompt 提示信息
     * @return 用户输入的整数
     */
    public static int getInt(String prompt) {
        int value = 0;
        boolean isValid = false;
        while (!isValid) {
            System.out.print(prompt);
            try {
                value = Integer.parseInt(scanner.nextLine());
                isValid = true;
            } catch (NumberFormatException e) {
                System.out.println("无效的输入，请输入一个整数。");
            }
        }
        return value;
    }
    public static String getString(String prompt, int minLength, int maxLength) {
        int retryCount = 0;
        while (retryCount < MAX_RETRY_TIMES) {
            System.out.print(prompt);
            String input = scanner.nextLine().trim();
            if (input.length() >= minLength && input.length() <= maxLength) {
                return input;
            }
            System.out.printf("输入长度必须在 %d 到 %d 之间，请重试。\n", minLength, maxLength);
            retryCount++;
        }
        throw new RuntimeException("已达到最大重试次数");
    }

    /**
     * 获取用户输入的字符串
     *
     * @param prompt 提示信息
     * @return 用户输入的字符串
     */
    public static String getString(String prompt) {
        System.out.print(prompt);
        return scanner.nextLine();
    }

    /**
     * 获取用户输入的浮点数
     *
     * @param prompt 提示信息
     * @return 用户输入的浮点数
     */
    public static double getDouble(String prompt) {
        double value = 0.0;
        boolean isValid = false;
        while (!isValid) {
            System.out.print(prompt);
            try {
                value = Double.parseDouble(scanner.nextLine());
                isValid = true;
            } catch (NumberFormatException e) {
                System.out.println("无效的输入，请输入一个浮点数。");
            }
        }
        return value;
    }
    public static double getDouble(String prompt, double min, double max) {
        int retryCount = 0;
        while (retryCount < MAX_RETRY_TIMES) {
            System.out.print(prompt);
            try {
                String input = scanner.nextLine().trim();
                if (input.isEmpty()) {
                    System.out.println("输入不能为空，请重试。");
                    retryCount++;
                    continue;
                }
                double value = Double.parseDouble(input);
                if (value >= min && value <= max) {
                    return value;
                }
                System.out.printf("输入必须在 %.2f 到 %.2f 之间，请重试。\n", min, max);
            } catch (NumberFormatException e) {
                System.out.println("无效的数字格式，请重试。");
            }
            retryCount++;
        }
        throw new RuntimeException("已达到最大重试次数");
    }

    /**
     * 获取用户输入的布尔值
     *
     * @param prompt 提示信息
     * @return 用户输入的布尔值
     */
    public static boolean getBoolean(String prompt) {
        boolean value = false;
        boolean isValid = false;
        while (!isValid) {
            System.out.print(prompt);
            String input = scanner.nextLine().trim().toLowerCase();
            if ("yes".equals(input) || "y".equals(input)) {
                value = true;
                isValid = true;
            } else if ("no".equals(input) || "n".equals(input)) {
                value = false;
                isValid = true;
            } else {
                System.out.println("无效的输入，请输入 yes/y 或 no/n。");
            }
        }
        return value;
    }

    /**
     * 关闭扫描器
     */
    public static void closeScanner() {
        scanner.close();
    }
}
